Trò chơi Tic-Tac-Toe, game đánh caro full source code
- AccountService.cs
- PhotonNetwork /
- Editor /
- Photon Unity Networking /
- Assets /
- project /
2 // <copyright file="AccountService.cs" company="Exit Games GmbH">
3 // Photon Cloud Account Service - Copyright (C) 2012 Exit Games GmbH
4 // </copyright>
5 // <summary>
6 // Provides methods to register a new user-account for the Photon Cloud and
7 // get the resulting appId.
8 // </summary>
9 // <author>developer@exitgames.com</author>
10 // ----------------------------------------------------------------------------
11
12 using System.Net.Security;
13 using System.Security.Cryptography.X509Certificates;
14
15
16 using System;
17 using System.Collections.Generic;
18 using System.IO;
19 using System.Net;
20
21 using Newtonsoft.Json;
22
23 public class AccountService
24 {
25 private const string ServiceUrl = "https://service.exitgames.com/AccountExt/AccountServiceExt.aspx";
26
27 private Action<AccountService> registrationCallback; // optional (when using async reg)
28
29 public string Message { get; private set; } // msg from server (in case of success, this is the appid)
30
31 protected internal Exception Exception { get; set; } // exceptions in account-server communication
32
33 public string AppId { get; private set; }
34
35 public int ReturnCode { get; private set; } // 0 = OK. anything else is a error with Message
36
37 public enum Origin : byte { ServerWeb = 1, CloudWeb = 2, Pun = 3, Playmaker = 4 };
38
39 /// <summary>
40 /// Creates a instance of the Account Service to register Photon Cloud accounts.
41 /// </summary>
42 public AccountService()
43 {
44 WebRequest.DefaultWebProxy = null;
45 ServicePointManager.ServerCertificateValidationCallback = Validator;
46 }
47
48 public static bool Validator(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors)
49 {
50 return true; // any certificate is ok in this case
51 }
52
53 /// <summary>
54 /// Attempts to create a Photon Cloud Account.
55 /// Check ReturnCode, Message and AppId to get the result of this attempt.
56 /// </summary>
57 /// <param name="email">Email of the account.</param>
58 /// <param name="origin">Marks which channel created the new account (if it's new).</param>
59 public void RegisterByEmail(string email, Origin origin)
60 {
61 this.registrationCallback = null;
62 this.AppId = string.Empty;
63 this.Message = string.Empty;
64 this.ReturnCode = -1;
65
66 string result;
67 try
68 {
69 WebRequest req = HttpWebRequest.Create(this.RegistrationUri(email, (byte)origin));
70 HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
71
72 // now read result
73 StreamReader reader = new StreamReader(resp.GetResponseStream());
74 result = reader.ReadToEnd();
75 }
76 catch (Exception ex)
77 {
78 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
79 this.Exception = ex;
80 return;
81 }
82
83 this.ParseResult(result);
84 }
85
86 /// <summary>
87 /// Attempts to create a Photon Cloud Account asynchronously.
88 /// Once your callback is called, check ReturnCode, Message and AppId to get the result of this attempt.
89 /// </summary>
90 /// <param name="email">Email of the account.</param>
91 /// <param name="origin">Marks which channel created the new account (if it's new).</param>
92 /// <param name="callback">Called when the result is available.</param>
93 public void RegisterByEmailAsync(string email, Origin origin, Action<AccountService> callback = null)
94 {
95 this.registrationCallback = callback;
96 this.AppId = string.Empty;
97 this.Message = string.Empty;
98 this.ReturnCode = -1;
99
100 try
101 {
102 HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(this.RegistrationUri(email, (byte)origin));
103 req.Timeout = 5000;
104 req.BeginGetResponse(this.OnRegisterByEmailCompleted, req);
105 }
106 catch (Exception ex)
107 {
108 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
109 this.Exception = ex;
110 if (this.registrationCallback != null)
111 {
112 this.registrationCallback(this);
113 }
114 }
115 }
116
117 /// <summary>
118 /// Internal callback with result of async HttpWebRequest (in RegisterByEmailAsync).
119 /// </summary>
120 /// <param name="ar"></param>
121 private void OnRegisterByEmailCompleted(IAsyncResult ar)
122 {
123 try
124 {
125 HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
126 HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;
127
128 if (response != null && response.StatusCode == HttpStatusCode.OK)
129 {
130 // no error. use the result
131 StreamReader reader = new StreamReader(response.GetResponseStream());
132 string result = reader.ReadToEnd();
133
134 this.ParseResult(result);
135 }
136 else
137 {
138 // a response but some error on server. show message
139 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
140 }
141 }
142 catch (Exception ex)
143 {
144 // not even a response. show message
145 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
146 this.Exception = ex;
147 }
148
149 if (this.registrationCallback != null)
150 {
151 this.registrationCallback(this);
152 }
153 }
154
155 /// <summary>
156 /// Creates the service-call Uri, escaping the email for security reasons.
157 /// </summary>
158 /// <param name="email">Email of the account.</param>
159 /// <param name="origin">1 = server-web, 2 = cloud-web, 3 = PUN, 4 = playmaker</param>
160 /// <returns>Uri to call.</returns>
161 private Uri RegistrationUri(string email, byte origin)
162 {
163 string emailEncoded = Uri.EscapeDataString(email);
164 string uriString = string.Format("{0}?email={1}&origin={2}", ServiceUrl, emailEncoded, origin);
165
166 return new Uri(uriString);
167 }
168
169 /// <summary>
170 /// Reads the Json response and applies it to local properties.
171 /// </summary>
172 /// <param name="result"></param>
173 private void ParseResult(string result)
174 {
175 if (string.IsNullOrEmpty(result))
176 {
177 this.Message = "Server's response was empty. Please register through account website during this service interruption.";
178 return;
179 }
180
181 Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(result);
182 if (values == null)
183 {
184 this.Message = "Service temporarily unavailable. Please register through account website.";
185 return;
186 }
187
188 int returnCodeInt = -1;
189 string returnCodeString = string.Empty;
190 string message;
191
192 values.TryGetValue("ReturnCode", out returnCodeString);
193 values.TryGetValue("Message", out message);
194 int.TryParse(returnCodeString, out returnCodeInt);
195
196 this.ReturnCode = returnCodeInt;
197 if (returnCodeInt == 0)
198 {
199 // returnCode == 0 means: all ok. message is new AppId
200 this.AppId = message;
201 }
202 else
203 {
204 // any error gives returnCode != 0
205 this.AppId = string.Empty;
206 this.Message = message;
207 }
208 }
209 }
----------------------------------------------------------------------------
Photon Cloud Account Service - Copyright (C) 2012 Exit Games GmbH
Provides methods to register a new user-account for the Photon Cloud and
get the resulting appId.
----------------------------------------------------------------------------
private const string ServiceUrl = "https:service.exitgames.comAccountExtAccountServiceExt.aspx";
private Action
public string Message { get; private set; } msg from server (in case of success, this is the appid)
protected internal Exception Exception { get; set; } exceptions in account-server communication
public int ReturnCode { get; private set; } 0 = OK. anything else is a error with Message
Creates a instance of the Account Service to register Photon Cloud accounts.
return true; any certificate is ok in this case
Attempts to create a Photon Cloud Account.
Check ReturnCode, Message and AppId to get the result of this attempt.
Email of the account.
Marks which channel created the new account (if it's new).
now read result
Attempts to create a Photon Cloud Account asynchronously.
Once your callback is called, check ReturnCode, Message and AppId to get the result of this attempt.
Email of the account.
Marks which channel created the new account (if it's new).
Called when the result is available.
Internal callback with result of async HttpWebRequest (in RegisterByEmailAsync).
no error. use the result
a response but some error on server. show message
not even a response. show message
Creates the service-call Uri, escaping the email for security reasons.
Email of the account.
1 = server-web, 2 = cloud-web, 3 = PUN, 4 = playmaker
Reads the Json response and applies it to local properties.
returnCode == 0 means: all ok. message is new AppId
any error gives returnCode != 0